# -*- coding: utf-8 -*-

import os
import cv2 as cv
import numpy as np

people = ["","",""]
DIR = r""

p = []
for i in os.listdir(DIR):
    p.append(i)
print(p)

haar_cascade = cv.CascadeClassifier("haarcascade_frontalface_default.xml")

features = []
labels = []

def create_train():
    for person in people:
        path = os.path.join(DIR, person)
        label = people.index(person)
        
        for img_filename in os.listdir(path):
            img_path = os.path.join(path, img_filename)
            
            img = cv.imread(img_path)
            gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
            
            face_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighors=3)
            for (x,y,w,h) in face_rect:
                face = gray[y:y+h, x:x+w]
                features.append(face)
                labels.append(label)
                
create_train()
print(f"length of the features = {len(features)}")
print(f"length of the labels = {len(labels)}")

features = np.array(features, dtype="object")
labels = np.array(labels)

face_recognizer = cv.face.LBPHFaceRecognizer_create()

# Train the recognizer on the features list and the labels list
face_recognizer.train(features, labels)

face_recognizer.save("face_train.yml")
np.save("features.npy", features)
np.sace("labels.npy", labels)